package com.tianyang.yukuaifu.business.trade.presenter;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tianyang.yukuaifu.business.trade.bean.TradeRecordOutput;
import com.tianyang.yukuaifu.business.trade.contract.SettleMethodContract;
import com.tianyang.yukuaifu.business.trade.model.TradeModel;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：结算方式
 * Created by xyj on 2024/3/11
 */
public class SettleMethodPresenter implements SettleMethodContract.Presenter {

    private SettleMethodContract.View mView;

    private CompositeSubscription mSubscription;

    public SettleMethodPresenter(SettleMethodContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getSettleMethodList(int pageNumber) {
        Subscription subscription = TradeModel.getInstance()
                .merchantDelayTradeDetail(pageNumber)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showSettleMethodListView(null, pageNumber == CommonConfig.PAGE_NUM);
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                    }

                    @Override
                    public void onNext(JSONObject outputs) {
                        List<TradeRecordOutput> recordOutputs = null;
                        int totalPage = 0;
                        if (outputs != null) {
                            recordOutputs = outputs.getObject("list", new TypeReference<List<TradeRecordOutput>>(){});
                            mView.showSettleTotalView(outputs.getJSONObject("obj"));
                            totalPage = outputs.getIntValue("totalPage");
                        }
                        mView.showSettleMethodListView(recordOutputs, pageNumber == CommonConfig.PAGE_NUM);
                        if (pageNumber < totalPage) {
                            mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
                        } else {
                            mView.showLoadMore(CommonConfig.LOADMOREEND);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
